﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using HRC.Library.EntityLibrary.EntityOperations;
using Lehnet.Models.DTO;

namespace Lehnet.BusinessLayer
{
    public class MusteriAdresiBusiness
    {
        public List<Models.DTO.MusteriAdres> GetMusteriAdresleriByMusteriId(int musteriId)
        {
            string conStr = HRC.Foundation.ConfigLibrary.ConfigManager.GetConfigValue<string>("conStr");
            var musteriAdresleri = new List<MusteriAdres>();
            var con = new SqlConnection(conStr);
            var cmd = new SqlCommand(@"select ma.Id,m.Ad as MusteriAd,case when ma.AdresTip = 1 then 'Yükleme Adresi' else 'Boşaltma Adresi' end as AdresTipi,ma.Adres,i.Ad as Il,ilc.Ad as Ilce,ma.IlgiliKisi,ma.Aciklama,ma.Aktif from musteriAdresleri ma
inner join musteriler m on ma.MusteriId = m.Id
inner join Iller i on ma.IlId = i.Id
inner join Ilceler ilc on ma.IlceId = ilc.Id
where ma.MusteriId=@MusteriId", con);
            cmd.Parameters.AddWithValue("@MusteriId", musteriId);
            using (con)
            {
                con.Open();
                using (var dr = cmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        musteriAdresleri.Add(DTOFacade.BindDTO<MusteriAdres>(dr));
                    }
                }
            }
            return musteriAdresleri;
        }

        public Models.MusteriAdres LoadMusteriAdresiById(int id)
        {
            return EntityManager.Load<Models.MusteriAdres>(" where Id=" + id);
        }

        public void SaveOrUpdate(Models.MusteriAdres musteriAdres)
        {
            if (musteriAdres.Id == 0)
            {
                EntityManager.Insert(musteriAdres);
            }
            else
            {
                EntityManager.Update(musteriAdres);
            }
        }
    }
}
